﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;

namespace RMSM
{
    [Serializable()]
    public class DMChungBOList : Csla.NameValueListBase<int?, string>
    {
        private static string whereDefaultDMChungBOList = "";

        #region Factory Methods
        private DMChungBOList()
        {
        }

        public void InsertCustomValue(int? customKey, string customValue)
        {
            IsReadOnly = false;
            this.Insert(0, new NameValuePair(customKey, customValue));
            IsReadOnly = true;
        }

        public void CleaAll()
        {
            IsReadOnly = false;
            this.ClearItems();
            IsReadOnly = true;
        }

        private static DMChungBOList _list;

        public static DMChungBOList GetDMChungBOList()
        {
            string where = "WHERE Deleted=0" + (whereDefaultDMChungBOList == "" ? "" : " AND " + whereDefaultDMChungBOList);
            SPParam spParam = new SPParam("NoiDung", "ASC", where);
            _list = DataPortal.Fetch<DMChungBOList>(spParam);
            return _list;
        }

        public static DMChungBOList GetDMChungBOList(LoaiDanhMuc loaiDanhMuc)
        {
            string where = "WHERE Deleted=0 AND LoaiDanhMuc=" + (int)loaiDanhMuc;
            SPParam spParam = new SPParam("NoiDung", "ASC", where);
            _list = DataPortal.Fetch<DMChungBOList>(spParam);
            return _list;
        }

        public static DMChungBOList GetDMChungBOListWithNullValue()
        {
            string where = "WHERE Deleted=0" + (whereDefaultDMChungBOList == "" ? "" : " AND " + whereDefaultDMChungBOList);
            SPParam spParam = new SPParam("NoiDung", "ASC", where);
            _list = DataPortal.Fetch<DMChungBOList>(spParam);
            _list.IsReadOnly = false;
            _list.Insert(0, new NameValuePair(null, "(Không)"));
            _list.IsReadOnly = true;
            return _list;
        }

        public static DMChungBOList GetDMChungBOListWithNullValue(string where)
        {
            where = "WHERE Deleted=0" + (where == "" ? "" : " AND " + where) + (whereDefaultDMChungBOList == "" ? "" : " AND " + whereDefaultDMChungBOList);
            SPParam spParam = new SPParam("NoiDung", "ASC", where);
            _list = DataPortal.Fetch<DMChungBOList>(spParam);
            _list.IsReadOnly = false;
            _list.Insert(0, new NameValuePair(null, "(Không)"));
            _list.IsReadOnly = true;
            return _list;
        }

        public static DMChungBOList GetDMChungBOList(bool getDeleted)
        {
            string where = (whereDefaultDMChungBOList == "" ? "" : "WHERE " + whereDefaultDMChungBOList);
            SPParam spParam = new SPParam("NoiDung", "ASC", where);
            _list = DataPortal.Fetch<DMChungBOList>(spParam);
            return _list;
        }

        public static DMChungBOList GetDMChungBOList(string where)
        {
            where = "WHERE Deleted=0" + (where == "" ? "" : " AND " + where) + (whereDefaultDMChungBOList == "" ? "" : " AND " + whereDefaultDMChungBOList);
            SPParam spParam = new SPParam("NoiDung", "ASC", where);
            _list = DataPortal.Fetch<DMChungBOList>(spParam);
            return _list;
        }

        public static DMChungBOList GetDMChungBOList(string where, bool getDeleted)
        {
            string tmpWhere = ((where != "" || whereDefaultDMChungBOList != "") ? "WHERE " : "");
            tmpWhere += (where == "" ? "" : where);
            if (whereDefaultDMChungBOList != "")
                tmpWhere += (where == "" ? "" : " AND ") + whereDefaultDMChungBOList;
            SPParam spParam = new SPParam("NoiDung", "ASC", tmpWhere);
            _list = DataPortal.Fetch<DMChungBOList>(spParam);
            return _list;
        }

        public static DMChungBOList GetDMChungBOList(string where, string orderByColumn)
        {
            where = "WHERE Deleted=0" + (where == "" ? "" : " AND " + where) + (whereDefaultDMChungBOList == "" ? "" : " AND " + whereDefaultDMChungBOList);
            SPParam spParam = new SPParam((orderByColumn == "" ? "NoiDung" : orderByColumn), "ASC", where);
            _list = DataPortal.Fetch<DMChungBOList>(spParam);
            return _list;
        }

        public static DMChungBOList GetDMChungBOList(string where, string orderByColumn, bool getDeleted)
        {
            string tmpWhere = ((where != "" || whereDefaultDMChungBOList != "") ? "WHERE " : "");
            tmpWhere += (where == "" ? "" : where);
            if (whereDefaultDMChungBOList != "")
                tmpWhere += (where == "" ? "" : " AND ") + whereDefaultDMChungBOList;
            SPParam spParam = new SPParam((orderByColumn == "" ? "NoiDung" : orderByColumn), "ASC", tmpWhere);
            _list = DataPortal.Fetch<DMChungBOList>(spParam);
            return _list;
        }

        public static DMChungBOList GetDMChungBOList(string where, string orderByColumn, string orderByExpression)
        {
            where = "WHERE Deleted=0" + (where == "" ? "" : " AND " + where) + (whereDefaultDMChungBOList == "" ? "" : " AND " + whereDefaultDMChungBOList);
            SPParam spParam = new SPParam((orderByColumn == "" ? "NoiDung" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), where);
            _list = DataPortal.Fetch<DMChungBOList>(spParam);
            return _list;
        }

        public static DMChungBOList GetDMChungBOList(string where, string orderByColumn, string orderByExpression, bool getDeleted)
        {
            string tmpWhere = ((where != "" || whereDefaultDMChungBOList != "") ? "WHERE " : "");
            tmpWhere += (where == "" ? "" : where);
            if (whereDefaultDMChungBOList != "")
                tmpWhere += (where == "" ? "" : " AND ") + whereDefaultDMChungBOList;
            SPParam spParam = new SPParam((orderByColumn == "" ? "NoiDung" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), tmpWhere);
            _list = DataPortal.Fetch<DMChungBOList>(spParam);
            return _list;
        }

        public static void InvalidateCache()
        {
            _list = null;
        }
        #endregion Factory Methods
        //==========================================================================

        #region Data Access
        protected override void DataPortal_Fetch(object _spParam)
        {
            SPParam spParam = (SPParam)_spParam;
            RaiseListChangedEvents = false;
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("GetBOList");
            db.AddInParameter(cmd, "@TableName", SqlDbType.VarChar, (object)"DMChung");
            db.AddInParameter(cmd, "@KeyColumn", SqlDbType.VarChar, (object)"DMChungID");
            db.AddInParameter(cmd, "@ValueColumn", SqlDbType.VarChar, (object)"NoiDung");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                IsReadOnly = false;
                while (dr.Read())
                    this.Add(new NameValuePair((int?)dr["DMChungID"], (string)dr["NoiDung"]));
                IsReadOnly = true;
            }
            RaiseListChangedEvents = true;
        }
        #endregion Data Access
        //==========================================================================
    }
}
